package com.wys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wys.entity.User;
import com.wys.response.BaseResponse;
import com.wys.service.IUserService;
import com.wys.utils.CodeUtil;
import com.wys.utils.MD5Util;
import com.wys.utils.ResponseParameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.RenderedImage;
import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping(value = "/jspdemo")
public class LoginController {
    @Autowired
    private IUserService userService;

    @RequestMapping(value = "/verifycode", name = "生成验证码")
    public void generateImgVerificationCode(HttpServletRequest req, HttpServletResponse resp){
        // 调用工具类生成的验证码和验证码图片
        Map<String, Object> codeMap = CodeUtil.generateCodeAndPic();

        // 将四位数字的验证码保存到Session中。
        HttpSession session = req.getSession();
        session.setAttribute("code", codeMap.get("code").toString());

        // 禁止图像缓存。
        resp.setHeader("Pragma", "no-cache");
        resp.setHeader("Cache-Control", "no-cache");
        resp.setDateHeader("Expires", -1);

        resp.setContentType("image/jpeg");

        // 将图像输出到Servlet输出流中。
        ServletOutputStream sos;
        try {
            sos = resp.getOutputStream();
            ImageIO.write((RenderedImage) codeMap.get("codePic"), "jpeg", sos);
            sos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/login", name = "跳转到登录界面")
    public String toLoginPage(){
        System.out.println("登录");
        return "/loginviews/login";
    }

    @RequestMapping(value = "/checkLogin", name = "登录检查")
    @ResponseBody
    public BaseResponse checkLogin(HttpServletRequest request){
        //username password captcha
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String verifyCode = request.getParameter("captcha");
        String code = (String) request.getSession().getAttribute("code");
        System.out.println(username+" "+password+ " "+verifyCode+" "+code);
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.lambda().eq(User::getUsername, username).eq(User::getPassword, MD5Util.getPwd(password));
        User user = userService.getOne(wrapper);
        if(user != null && code.equals(verifyCode)){
            return new BaseResponse(ResponseParameter.CODE_SUCCESS, ResponseParameter.MSG_SUCCESS, user.getId());
        }
        return new BaseResponse(ResponseParameter.CODE_ERROR, ResponseParameter.MSG_ERROR, null);
    }

    @RequestMapping(value = "/index", name = "跳转到主页面")
    public String toIndexPage(Integer id, Model model){
        User user = userService.getById(id);
        model.addAttribute("user", user);
        return "/indexviews/index";
    }





}
